/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package com.gitee.wsl.platform.network

import java.net.*

/**
 * Represents remote endpoint with [hostname] and [port].
 *
 * The address will be resolved after construction.
 *
 * @throws UnresolvedAddressException if the [hostname] cannot be resolved.
 */
 actual typealias NetworkAddress = SocketAddress

 actual val NetworkAddress.hostname: String
    get() = (this as? InetSocketAddress)?.hostName ?: (this as? InetSocketAddress)?.address?.hostName ?: ""

 actual val NetworkAddress.address: String
    get() = (this as? InetSocketAddress)?.hostString ?: ""

 actual val NetworkAddress.port: Int
    get() = (this as? InetSocketAddress)?.port ?: 0

 actual typealias UnresolvedAddressException = java.nio.channels.UnresolvedAddressException

 actual fun NetworkAddress(hostname: String, port: Int): NetworkAddress {
    return InetSocketAddress(hostname, port)
}
